package com.iyjhsoc.game.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.iyjhsoc.game.ENUM.ConstantENUM;
import com.iyjhsoc.game.dao.CompetitionDAO;
import com.iyjhsoc.game.dao.CompetitionSmallGameDAO;
import com.iyjhsoc.game.domain.VO.CompetitionSmallGameVO;
import com.iyjhsoc.game.domain.VO.CompetitionVO;
import com.iyjhsoc.game.domain.dto.CompetitionSmallGameDTO;
import com.iyjhsoc.game.domain.dto.KillOrTimeDTO;
import com.iyjhsoc.game.domain.entity.CompetitionDO;
import com.iyjhsoc.game.domain.entity.CompetitionSmallGameDO;
import com.iyjhsoc.game.domain.response.Response;
import com.iyjhsoc.game.service.CompetitionSmallGameService;
import com.iyjhsoc.game.util.GameUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompetitionSmallGameServiceImpl extends ServiceImpl<CompetitionSmallGameDAO,CompetitionSmallGameDO> implements CompetitionSmallGameService  {
    @Resource
    CompetitionSmallGameDAO competitionSmallGameDAO;
    @Resource
    CompetitionDAO competitionDAO;
    @Override
    public Response queryList(CompetitionSmallGameVO vo) {
        List<CompetitionSmallGameVO> queryList = competitionSmallGameDAO.queryList(vo);
        return Response.SUCCESS(queryList);
    }

    @Override
    public Response saveCompetitionSmall(CompetitionSmallGameVO vo) {
        Integer matchTime = 0;
        if(vo.getVictoryTeamId() == null){
            return Response.ERROR("胜利队伍不能为空");
        }
        if(StringUtil.isNotEmpty(vo.getMatchTime()) && vo.getMatchTime().contains(":")){
            try {
                matchTime = GameUtil.getMatchSecond(vo.getMatchTime());
            }catch (Exception e){
                return Response.ERROR("比赛时长格式错误请使用HH:mm:ss");
            }
        } else {
            return Response.ERROR("比赛时常错误");
        }
        Integer integer = competitionSmallGameDAO.selectCompetitionSmallGame(vo.getCompetitionId());
        CompetitionDO competitionDO = competitionDAO.selectById(vo.getCompetitionId());
        if(integer.equals(competitionDO.getGameNum())){
            return Response.ERROR("已记录"+integer+"次记录，无法记录超过小局数");
        }
        if(integer>competitionDO.getGameNum()){
            return Response.ERROR(ConstantENUM.DATA_ERROR);
        }
        CompetitionSmallGameDO competitionSmallGameDO = new CompetitionSmallGameDO();
        BeanUtils.copyProperties(vo,competitionSmallGameDO);
        competitionSmallGameDO.setMatchTime(matchTime.toString());
        if(vo.getVictoryTeamId().equals(competitionDO.getTeamOneId())){
            competitionSmallGameDO.setFailTeamId(competitionDO.getTeamTowId());
        } else {
            competitionSmallGameDO.setFailTeamId(competitionDO.getTeamOneId());
        }
        int insert = competitionSmallGameDAO.insert(competitionSmallGameDO);
        if(insert == 1){
            return Response.SUCCESS(ConstantENUM.SUCCESS);
        }
        return Response.ERROR(ConstantENUM.SAVE_ERROR);
    }

    @Override
    public Response updateCompetitionSmall(CompetitionSmallGameVO vo) {
        CompetitionSmallGameDO competitionSmallGameDO = competitionSmallGameDAO.selectById(vo.getId());
        if(competitionSmallGameDO == null){
            return Response.ERROR(ConstantENUM.DATA_ERROR);
        }
        Integer matchTime = 0;
        if(StringUtil.isNotEmpty(vo.getMatchTime()) && vo.getMatchTime().contains(":")){
            try {
                matchTime = GameUtil.getMatchSecond(vo.getMatchTime());
            }catch (Exception e){
                return Response.ERROR("比赛时长格式错误请使用HH:mm:ss");
            }
        } else {
            return Response.ERROR("比赛时常错误");
        }
        BeanUtils.copyProperties(vo,competitionSmallGameDO);
        CompetitionDO competitionDO = competitionDAO.selectById(vo.getCompetitionId());
        competitionSmallGameDO.setMatchTime(matchTime.toString());
        if(vo.getVictoryTeamId().equals(competitionDO.getTeamOneId())){
            competitionSmallGameDO.setFailTeamId(competitionDO.getTeamTowId());
        } else {
            competitionSmallGameDO.setFailTeamId(competitionDO.getTeamOneId());
        }
        int i = competitionSmallGameDAO.updateById(competitionSmallGameDO);
        if(i == 1){
            return Response.SUCCESS(ConstantENUM.UPDATE_SUCCESS);
        }
        return Response.ERROR(ConstantENUM.UPDATE_ERROR);
    }

    @Override
    public Response update(CompetitionSmallGameDO competitionDO) {
        return null;
    }

    @Override
    public Response deleteById(Long id) {
        return null;
    }

    @Override
    public Response queryCompetitionGame(CompetitionSmallGameVO vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<CompetitionSmallGameVO> list = competitionSmallGameDAO.queryCompetitionToGame(vo);
        return Response.SUCCESS(new PageInfo<>(list));
    }

    @Override
    public Response getOneKillRare(String name1, String name2) {
        Integer integer = competitionSmallGameDAO.selectCompetitionSixOneKillRare(name1, "1,2");
        return null;
    }

    @Override
    public Response selectGameValue(CompetitionSmallGameVO vo) {
        if(vo.getTeamId() == null){
            return Response.ERROR("比赛Id不能为空");
        }
        List<CompetitionSmallGameDTO> teamA = competitionSmallGameDAO.selectGameValue(vo);
        Map<Long,KillOrTimeDTO> listMap = new HashMap<>();
        KillOrTimeDTO teamARes = countCompetition(teamA);
        return Response.SUCCESS(teamARes);
    }

    @Override
    public Response selectGameInfo(CompetitionSmallGameVO vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<CompetitionSmallGameVO> list = competitionSmallGameDAO.queryCompetitionToGame(vo);
        return Response.SUCCESS(new PageInfo<>(list));
    }

    @Override
    public Response queryGameBPInfo(CompetitionSmallGameVO vo) {
        return null;
    }

    public KillOrTimeDTO countCompetition(List<CompetitionSmallGameDTO> dtos){
         Integer fifteenTime = 0;
         Integer sixTime = 0;
         Integer sevenTime = 0;
         Integer fifteenKill = 0;
         Integer sixKill = 0;
         Integer sevenKill = 0;
         for(CompetitionSmallGameDTO smallGameDTO: dtos){;
             if(smallGameDTO.getMatchTime()>=900){
                 fifteenTime++;
             }
             if(smallGameDTO.getMatchTime() >= 960){
                 sixTime++;
             }
             if(smallGameDTO.getMatchTime() >= 1020){
                 sevenTime++;
             }
             if(smallGameDTO.getTotalKill() >= 15){
                 fifteenKill++;
             }
             if(smallGameDTO.getTotalKill() >= 16){
                 sixKill++;
             }
             if(smallGameDTO.getTotalKill() >= 17){
                 sevenKill++;
             }
        }
        KillOrTimeDTO team= new KillOrTimeDTO();
        team.setFifteenTime(fifteenTime);
        team.setFifteenKill(fifteenKill);
        team.setSixKill(sixKill);
        team.setSixTime(sixTime);
        team.setSevenKill(sevenKill);
        team.setSevenTime(sevenTime);
        team.setTotal(dtos.size());
        return team;
    }


}
